package com.naiterui.ehp.bs.recommend.repository.mysql;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bs.recommend.bean.RecomAuditLog;

@Repository
public interface IRecomAuditLogRepository extends BaseJpaRepository<RecomAuditLog, Long> {

  RecomAuditLog findFirstByRecomId(Long recomId);

  List<RecomAuditLog> findByStatusAndPharmacistIdAndRecomIdIn(Integer status, Long pharmacistId, List<Long> recomId);

  List<RecomAuditLog> findByStatusAndPharmacistIdGreaterThanAndChangedAtBeforeAndRecomIdIn(Integer status,
      Long pharmacistId, Date changedAt, List<Long> recomId);

  /**
   * 查询超时未审核处方
   *
   * @return
   */
  @Query(value = "select a.* from recom_audit_log a join dr_medication_recom r  on r.id=a.recom_id and a.status=0 and r.expire_at < now() ", nativeQuery = true)
  List<RecomAuditLog> getExpiredRecom();

  /**
   * 查询未审核的处方数
   *
   * @param status
   * @return
   */
  long countByStatus(Integer status);

  List<RecomAuditLog> findByAuditTypeAndStatusAndChangedAtBetween(Integer auditType, Integer status, Date startDate,
      Date endDate);
}
